#include "stdafx.h"
#include "IOFile.h"


CIOFile::CIOFile(void)
{
}


CIOFile::~CIOFile(void)
{
}

CString CIOFile::GetInputPath()
{
	return m_sInputFilePath;
}

CString CIOFile::GetOutputPath()
{
	return m_sOutputFilePath;
}

VOID CIOFile::SetInputPath(CString inputFilePath)
{
	m_sInputFilePath = inputFilePath;	
}

VOID CIOFile::SetOutputPath(CString outputFilePath)
{
	m_sOutputFilePath = outputFilePath;
}

INT CIOFile::ReadDataFromFile(INPUT_DATA &inputData, LABEL_DATA &labelData)
{
	if(m_sOutputFilePath.IsEmpty() || PathFileExists(m_sOutputFilePath))
	{
		AfxMessageBox(IDS_INPUT_FILE_NOT_EXIST);
		return -1;	// Error: Input file not exist
	}
	
	CStdioFile inputFile;
	CString sLineText;

	if (inputFile.Open(m_sInputFilePath, CStdioFile::modeRead))
	{
		CStringArray sArray;
		CString sTemp, sSeperator = _T("\t");
		int iPosition = 0, iDimension = 0;
		

		// Read the first line. Give number of dimensions of data. 
		inputFile.ReadString(sLineText);
		if(sLineText.Find(sSeperator, 0) == -1)
			sSeperator = _T(" ");
		if(sLineText.Find(sSeperator, 0) == -1)
			sSeperator = _T(",");

		do
		{
			sTemp = sLineText.Tokenize(sSeperator, iPosition);
			if(!sTemp.IsEmpty())
				sArray.Add(sTemp);
			else
				break;
		}while(1);

		iDimension = sArray.GetSize() - 2; //

		while (inputFile.ReadString(sLineText))
		{
			CARRAY_FLOAT cfTemp;
			// Tach ID
			iPosition = 0;
			sTemp = sLineText.Tokenize(sSeperator, iPosition);
			// Tach so lieu
			for(int i = 0; i < iDimension; i++)
			{
				sTemp = sLineText.Tokenize(sSeperator, iPosition);
				cfTemp.push_back((float)_ttof(sTemp));
			}
			// Tach lay label
			sTemp = sLineText.Tokenize(sSeperator, iPosition);
			if(sTemp.IsEmpty())
			{
				AfxMessageBox(IDS_INPUT_FILE_ERROR);
				return -2;	// File format's error.
			}
			else
			{
				// Add to label array.
				labelData.push_back(sTemp);
				// Add to inputData array
				inputData.push_back(cfTemp);
			}
		}

		inputFile.Close();
		return labelData.size();	// Number of data sample read
		
	}
	AfxMessageBox(IDS_INPUT_FILE_OPEN_ERROR);
	return -3;		// Error to open file
}

BOOL CIOFile::WriteResult(OUTPUT_DATA outputData, LABEL_DATA labelData)
{
	if(m_sOutputFilePath.IsEmpty() || PathFileExists(m_sOutputFilePath))
	{
		m_sOutputFilePath = _T("C:\\Users\\Computer\\Desktop\\output.txt");
	}

	CStdioFile outputFile;
	CString sLineText;
	unsigned int iNumberOfCluster = outputData.size(), iNumberOfSample;

	if (!outputFile.Open(m_sOutputFilePath, CStdioFile::modeCreate | CStdioFile::modeWrite))
	{
		AfxMessageBox(IDS_OUTPUT_FILE_OPEN_ERROR);
		return FALSE;  // error to open file. Can't write
	}

	for(unsigned i = 0; i < iNumberOfCluster; i++)
	{
		sLineText.Format(_T("****Cluster %3d*****\n"), i+1);
		iNumberOfSample = outputData[i].size();
		for(unsigned int j = 0; j < iNumberOfSample; j++)
		{
			sLineText.Append(labelData[outputData[i][j]] + _T("\t"));
		}
		sLineText.Append(_T("\n"));
		outputFile.WriteString(sLineText);
	}
	
	outputFile.Close();
	return TRUE; // Wrote data to output file
}
